package bestfriends.tamu;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

//Read and write to XML database using xstream

public class Compare extends Activity {
	ArrayList<Person> people;
	ArrayList<Question> questions;
	int questionIndex;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.compare);
        
        AssetManager am = getApplicationContext().getAssets();
        Random rand = new Random();
        
        ImageButton left = (ImageButton)findViewById(R.id.imageButton1);
        ImageButton right = (ImageButton)findViewById(R.id.ImageButton01);
        TextView question = (TextView)findViewById(R.id.textView1);
        
        questionIndex = rand.nextInt(ListManager.questions.size());
        
        Question currentQuestion = ListManager.questions.get(questionIndex);
        question.setText(currentQuestion.getQuestionText());        
        
        try {
			String[] pics = am.list("faces");
			ArrayList<String> paths = new ArrayList<String>(Arrays.asList(pics));
			
	        int i1 = rand.nextInt(paths.size())+1;
	        int i2 = i1;
        	while(i1 == i2){
        		i2 = rand.nextInt(paths.size())+1;
        	}
	        
	        String face1 = "face" + i1 + ".jpg";
	        String face2 = "face" + i2 + ".jpg";
	        
	        
	        
	        InputStream ims1 = am.open("faces/"+face1);
	        Drawable d1 = Drawable.createFromStream(ims1,  null);
	        left.setImageDrawable(d1);
	        left.setTag(i1); // set tags to get the face id for logging for the server
	        ims1.close();
	        //this set tag might be causing the problems
	        
	        InputStream ims2 = am.open("faces/"+face2);
	        Drawable d2 = Drawable.createFromStream(ims2,  null);
	        right.setImageDrawable(d2);
	        right.setTag(i2);
	        ims2.close();
	        
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.compare, menu);
        return true;
    }
    
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	ListManager.writeArraysToSD();
    }
    
    public void loadHighScores(View v) {
    	Intent i = new Intent(Compare.this, HighScores.class );
    	Compare.this.startActivity(i);
    }
    
    public void optionOne(View v) {
    	// save the win and opponent loss
    	int winTag = (Integer)v.getTag();
    	ImageButton loser = (ImageButton) findViewById(R.id.ImageButton01);    	
    	int loseTag = (Integer) loser.getTag();
    	
    	//This could be the issue here
    	for(Result r : ListManager.questions.get(questionIndex).getResult()) {
    		if(r.id == winTag) {
    			r.setAppearances(r.getAppearances() + 1); 
    			r.setWins(r.getWins() + 1);
    		}
    		
    		if(r.id == loseTag) {
    			r.setAppearances(r.getAppearances() + 1); 
    		}
    	}  
    	
    	Intent i = new Intent(Compare.this, Compare.class );
    	Compare.this.startActivity(i);
    }
    
    public void optionTwo(View v) {
    	// save the win and opponent loss
    	int winTag = (Integer)v.getTag();
    	ImageButton loser = (ImageButton) findViewById(R.id.imageButton1);    	
    	int loseTag = (Integer) loser.getTag();
    	try{
    	for(Result r : ListManager.questions.get(questionIndex).getResult()) {
    		if(r.id == winTag) {
    			r.setAppearances(r.getAppearances() + 1); 
    			r.setWins(r.getWins() + 1);
    		}
    		
    		if(r.id == loseTag) {
    			r.setAppearances(r.getAppearances() + 1); 
    		}
    	}
    	}
    	
    	catch (Exception e){
    		e.printStackTrace();
    	}
    	Intent i = new Intent(Compare.this, Compare.class );
    	Compare.this.startActivity(i);
    }
    
    public void skipQuestion(View v){
    	Intent i = new Intent(Compare.this, Compare.class );
    	Compare.this.startActivity(i);
    }
}
